#! /bin/bash
# Install the repository RPM:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Disable the built-in PostgreSQL module:
sudo dnf -qy module disable postgresql

# Install PostgreSQL:
sudo dnf install -y postgresql15-server

sudo mkdir /etc/systemd/system/postgresql-15.service.d
{ echo "[Service]";
    echo "Environment=PGDATA=/opt/data/psql/15";
} | sudo tee /etc/systemd/system/postgresql-15.service.d/override.conf
sudo mkdir -p /opt/data/psql/15
sudo chown postgres:postgres /opt/data/psql/15


# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15

# добавить пользователя
sudo su - -c "psql -c \"CREATE USER semyon WITH SUPERUSER PASSWORD 'jw8s0F4';\"" postgres

#
# добавить пользователя
sudo su - -c "psql -c \"create database mytest;\"" postgres
# Разрешить подключения новым пользователем
sudo echo "host    all             semyon          0.0.0.0/0               scram-sha-256" | sudo tee -a /opt/data/psql/15/pg_hba.conf
# Запустить ДБ на всех интерфейсах
sudo echo "listen_addresses = '*'" | sudo tee -a /opt/data/psql/15/postgresql.conf


#
sudo systemctl restart postgresql-15

